<?php
$pathArr = explode("/", $_SERVER["PHP_SELF"]);
$contextPath = $pathArr[1];
$rootPath = $_SERVER["DOCUMENT_ROOT"];

include $rootPath . "/" . $contextPath . "/apps/ManagerIUDS.php";
include $rootPath . "/" . $contextPath . "/apps/FreeSql.php";
$managerIUDS = new ManagerIUDS();
$freeSql = new FreeSql();
$page = $_GET['page'];
if (!isset($page) && $page == "") {
	$page = 1;
}

$mode=$_GET['mode'];
if (isset($mode) && $mode == "search"){
	$petName = $_GET['pet_name'];
	$petData = $managerIUDS->queryData("pet_id", "pet", " WHERE pet_name LIKE '%".$petName."%' ", "2", "");
	if(sizeof($petData)>0){
		$condition = " WHERE ";
		foreach($petData as $value){
			$condition .=" pet_id =".$value["pet_id"]." OR ";
		}
		$condition = subStr($condition,0,-4);
		$condition .= " AND history_type	='M'";
	}else{
		$condition ="";
	}
	$listMaintenance = $managerIUDS->listQueryLimit($page, "history", "history_id",$condition);
}else {
	$listMaintenance = $managerIUDS->listQueryLimit($page, "history", "history_id"," WHERE history_type	='M'");
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>.: Predicate List :.</title>
<?php
require_once("../includes/css_inc.php");
require_once("../includes/js_inc.php");
?>
<script>
			$(document).ready(function(){ 
				$("#insert").click(function(){ 
					window.location = "pet_detail.php?mode=insert";
				});
				$("#pet_name").autocomplete({
					source: function( request, response ) {
		                $.ajax({
							type: "GET",
							url: "../actions/pet_action.php",
							data: {mode : "listPetData",pet_name : $("#pet_name").val()},
							contentType: "application/json; charset=utf-8", 
							dataType: "json",
		                    success: function( data ) {
			                        response( $.map( data, function( item ) {
			                            return {
			                                label: item.pet_name,
			                                value: item.pet_name
			                            }
			                        }));
		                    }
		                });
		            },
					minLength: 2
				}); 
			});
		</script>
</head>
<body>
<form name="frm1" id="frm1" action="../actions/maintenance_action.php" method="get">
	<table width="100%" border="0">
		<tr>
        	<td align="right"> ชื่อสัตว์เลี้ยง : 
        		<input type="text" name="pet_name" id="pet_name" class="higthInput">
				<input type="submit" name="search" id="search" value="ค้นหา">
			</td>
         </tr>	
	</table>
	<input type="hidden" name="mode" id="mode" value="searchPetByNameModeMaintenance">
</form>
<table style="width: 100%">
	<thead class="ui-widget">
		<tr class="ui-widget-header">
			<td width="2%" align="center">ลำดับ</td>
			<td width="3%" align="center">ชื่อสัตว์เลี้ยง</td>
			<td width="3%" align="center">วันที่รักษา</td>
			<td width="10%" align="center">ผลการรักษา</td>
			<td width="7%" align="center">รายการรักษา</td>
			<td width="7%" align="center">รายการยา</td>
		</tr>
	</thead>
	<tbody class="ui-widget-content">
	<?php
	$i = $listPredicate->startLimit;
	$r = 0;
	$i = ((($page*10)+1)-10);
	if(sizeof($listMaintenance) > 0){
		foreach ($listMaintenance as $value) {
			?>
		<tr>
			<td width="2%" align="center"><?=$i?></td>
			<td width="3%" align="center"><?php 
				$petData = $managerIUDS->queryData("pet_name", "pet", " WHERE pet_id='".$value["pet_id"]."'", "1", "");
				echo $petData["pet_name"];
			?></td>
			<td width="3%" align="center"><?=$value["maintenance_date"]?></td>
			<td width="10%" align="center"><?=$value["maintenance_detail"]?></td>
			<td width="7%" align="center"><?php
				$medicalCareList = explode(",",$value["medical_care_data"]);
				$medicalCareArray = serialize($medicalCareList);
				$myMedicalCareArray = unserialize($medicalCareArray);
				for($j=0;$j<sizeOf($myMedicalCareArray);$j++){
						$medicalCareData = $managerIUDS->queryData("medical_care_name", "medical_care", " WHERE medical_care_id='".$myMedicalCareArray[$j]."'", "1", "");
						echo ($j+1)." - ".$medicalCareData["medical_care_name"]."<br>";
				}
			?>
			<td width="7%" align="center"><?php
				$medicineList = explode(",",$value["medicine_data"]);
				$medicineArray = serialize($medicineList);
				$myNewArray = unserialize($medicineArray);
				$unit = "";
				for($j=0;$j<sizeOf($myNewArray);$j++){
						$medicineArrData = explode(":",$myNewArray[$j]);
						if($medicineArrData[1] == "S"){
							$unit = "เม็ด";
						}else if($medicineArrData[1] == "I"){
							$unit = "เข็ม";
						}else{
							$unit = "ขวด";
						}
						$medicineData = $managerIUDS->queryData("medicine_name", "medicine", " WHERE medicine_id='".$medicineArrData[0]."'", "1", "");
						echo ($j+1)." - ".$medicineData["medicine_name"]." จำนวน ".$medicineArrData[1]." ".$unit." <br>";
				}
			?>
		<?php
		$i++; $r++;}
	}else{
			if($mode!="search"){
			?>
			<tr>
				<td colspan="9" align="center"><font size="2" color="red">ไม่มีข้อมูล!!!</font></td>
			</tr>
			<?php
			}else{
			?>
			<tr>
				<td colspan="9" align="center"><font size="2" color="red">ไม่พบข้อมูลที่ค้นหา</font></td>
			</tr>
			<?php
			}
		}
		?>
		<tr>
			<td align="center" colspan="11"><?=$managerIUDS->buildPageBtn($page, $managerIUDS->getMaxPageByColumnName("history", "history_id"))?></td>
		</tr>
	</tbody>
</table>
</body>
</html>
